using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shooter : MonoBehaviour
{
    public GameObject bulletPrefab;
    public int shootCount;
    public float intervalAngle;
    public float intervalTime;
    float startAngle;
    public float comboTime;
    List<GameObject> bulletList;
    public Transform root;

    Camera cam;
    Vector3 dir;

    private void Start()
    {
        cam = Camera.main;
        bulletList = new List<GameObject>();
    }

    void Update()
    {
        dir = (Input.mousePosition - cam.WorldToScreenPoint(transform.position));
        dir.z = 0;
        transform.right = dir.normalized;
    }

    public void Shoot(Vector3 spawnPos)
    {
        startAngle = intervalAngle * (shootCount - 1) / 2;
        for (int i = 0; i < shootCount; i++)
        {
            Instantiate(bulletPrefab, spawnPos, Quaternion.Euler(0,0,startAngle+transform.eulerAngles.z), root);
            startAngle -= intervalAngle;
        }
    }

    IEnumerator Shoot()
    {
        bulletList.Clear();
        startAngle = intervalAngle * (shootCount - 1) / 2;
        for (int i = 0; i < shootCount; i++)
        {
            bulletList.Add(Instantiate(bulletPrefab, transform.position, Quaternion.Euler(0, 0, startAngle + transform.eulerAngles.z), root));
            startAngle -= intervalAngle;
        }
        yield return new WaitForSeconds(comboTime);
        foreach (var bullet in bulletList)
        {
            Shoot(bullet.transform.position);
        }
    }

    IEnumerator IntervalShoot()
    {
        startAngle = intervalAngle * (shootCount - 1) / 2;
        for (int i = 0; i < shootCount; i++)
        {
            Instantiate(bulletPrefab, transform.position, Quaternion.Euler(0, 0, startAngle), root);
            startAngle -= intervalAngle;
            yield return new WaitForSeconds(intervalTime);
        }
    }
}
